package wellness.activities;

import java.util.Date;

import wellness.activities.adapters.SemanasAdapter;
import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.TempoLivreBean;
import wellness.activities.data.TempoLivreData;
import wellness.activities.util.Dialogs;
import wellness.activities.util.Utilidades;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

public class NewFreeTime extends Activity {
	
	Context context;
	Spinner semanas;
	Button das;
	Button as;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_free_time);
		context = this;
		
		ImageView goBack = (ImageView) findViewById(R.id.imageViewGoBack);
		goBack.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		Intent intent = new Intent();
        		setResult(RESULT_OK, intent);
                finish();
            }
        });
        
		ImageView goHome = (ImageView) findViewById(R.id.imageViewGoHome);
        goHome.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		Intent intent = new Intent(NewFreeTime.this, Main.class);
        		startActivity(intent);
            }
        });
        
    	ImageView adicionar = (ImageView) findViewById(R.id.imageViewSaveNewTempoLivre);
    	adicionar.setOnClickListener(new View.OnClickListener() {
            
        	public void onClick(View view) {
        		adicionarTempoLivre();
            }
        });
        
        semanas = (Spinner) findViewById(R.id.spinnerSemanas);
        SemanasAdapter s = new SemanasAdapter(this);
        semanas.setAdapter(s);
        
        Date dataAtual = new Date();
        int hora = dataAtual.getHours();
        int minuto = dataAtual.getMinutes();
        
        das = (Button) findViewById(R.id.buttonDataDas);
        das.setText(Utilidades.getNumeroFormatado(hora) + ":" + Utilidades.getNumeroFormatado(minuto));
        das.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Dialogs.showDialogTime(v);
			}
		});
        
        as = (Button) findViewById(R.id.buttonDataAs);
        as.setText(Utilidades.getNumeroFormatado(hora) + ":" + Utilidades.getNumeroFormatado(minuto));
        as.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Dialogs.showDialogTime(v);
			}
		});
  
	}
	
	public void adicionarTempoLivre(){
		String semana = semanas.getSelectedItem().toString();
		String d = das.getText().toString();
		String s = as.getText().toString();
		TempoLivreBean tempo = new TempoLivreBean(semana, d, s);
		
		TempoLivreData data = new TempoLivreData(context);
		data.insert(tempo);
		
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
        finish();
	}

}
